package com.dengpp.pdform.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.dengpp.pdform.R;
import com.dengpp.pdform.base.BaseAttributeViewHolder;
import com.dengpp.pdform.config.AttributeConfig;
import com.dengpp.pdform.holder.CascadingDropdownViewHolder;
import com.dengpp.pdform.holder.CheckboxViewHolder;
import com.dengpp.pdform.holder.CustomViewHolder;
import com.dengpp.pdform.holder.DateViewHolder;
import com.dengpp.pdform.holder.DropdownViewHolder;
import com.dengpp.pdform.holder.MediaViewHolder;
import com.dengpp.pdform.holder.MultiLineViewHideHolder;
import com.dengpp.pdform.holder.MultiLineViewHolder;
import com.dengpp.pdform.holder.NumberViewHideHolder;
import com.dengpp.pdform.holder.NumberViewHolder;
import com.dengpp.pdform.holder.RadioViewHolder;
import com.dengpp.pdform.holder.TextViewHideHolder;
import com.dengpp.pdform.holder.TextViewHolder;
import com.dengpp.pdform.model.MediaFileDialog;

import java.util.List;

public class AttributeAdapter extends RecyclerView.Adapter<BaseAttributeViewHolder> {
    private List<AttributeConfig> items;
    private boolean isEditMode;
    private OnAttributeChangeListener listener;

    public AttributeAdapter(List<AttributeConfig> items, boolean isEditMode, OnAttributeChangeListener listener) {
        this.items = items;
        this.isEditMode = isEditMode;
        this.listener = listener;
    }

    @NonNull
    @Override
    public BaseAttributeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;

        switch (AttributeConfig.AttributeType.values()[viewType]) {
            case TEXT:
                view = inflater.inflate(R.layout.library_item_attribute_text, parent, false);
                return new TextViewHolder(view);
            case TEXT_HIDE:
                view = inflater.inflate(R.layout.library_item_attribute_text, parent, false);
                return new TextViewHideHolder(view);
            case MULTI_LINE_TEXT:
                view = inflater.inflate(R.layout.library_item_attribute_multiline, parent, false);
                return new MultiLineViewHolder(view);
            case MULTI_LINE_TEXT_HIDE:
                view = inflater.inflate(R.layout.library_item_attribute_multiline, parent, false);
                return new MultiLineViewHideHolder(view);
            case NUMBER:
                view = inflater.inflate(R.layout.library_item_attribute_text, parent, false);
                return new NumberViewHolder(view);
            case NUMBER_HIDE:
                view = inflater.inflate(R.layout.library_item_attribute_text, parent, false);
                return new NumberViewHideHolder(view);
            case RADIO:
                view = inflater.inflate(R.layout.library_item_attribute_radio, parent, false);
                return new RadioViewHolder(view);
            case CHECKBOX:
                view = inflater.inflate(R.layout.library_item_attribute_checkbox, parent, false);
                return new CheckboxViewHolder(view);
            case DATE:
                view = inflater.inflate(R.layout.library_item_attribute_date, parent, false);
                return new DateViewHolder(view);
            case DROPDOWN:
                view = inflater.inflate(R.layout.library_item_attribute_dropdown, parent, false);
                return new DropdownViewHolder(view);
            case MEDIA:
                view = inflater.inflate(R.layout.library_item_attribute_media, parent, false);
                return new MediaViewHolder(view);
            case CASCADING_DROPDOWN:
                view = inflater.inflate(R.layout.library_item_attribute_cascading_dropdown, parent, false);
                return new CascadingDropdownViewHolder(view);
            case CUSTOM_VIEW:
                view = inflater.inflate(R.layout.item_custom_view, parent, false);
                return new CustomViewHolder(view);
            default:
                view = inflater.inflate(R.layout.library_item_attribute_text, parent, false);
                return new TextViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull BaseAttributeViewHolder holder, int position) {
        holder.bind(items.get(position), isEditMode, listener);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override
    public int getItemViewType(int position) {
        return items.get(position).getType().ordinal();
    }

    @Override
    public void onViewRecycled(@NonNull BaseAttributeViewHolder holder) {
        super.onViewRecycled(holder);
        holder.onViewRecycled();
    }

    public interface OnAttributeChangeListener {
        void onAttributeChanged(String key, Object value);

        void onMediaAddRequested(String key, MediaFileDialog mediaFileDialog);

        void onMediaRemoveRequested(String key, int position);
    }
}